using FrameworkDesign;
using System.Collections.Generic;
using System.Linq;

namespace ShootingEditor2D
{
    public interface IGunSystem : ISystem 
    {
        GunInfo CurrentGun { get; }
        
        Queue<GunInfo> GunInfos { get; }
        
        void PickGun(string name, int bulletCountInGun, int bulletCountOutGun);
        
        void ShiftGun();
    }
    
    public class OnCurrentGunChanged
    {
        public string Name { get; set; }
    }

    public class GunSystem : AbstractSystem, IGunSystem
    {
        protected override void OnInit()
        {
      
        }
        
        private Queue<GunInfo> mGunInfos = new Queue<GunInfo>();
        
        public Queue<GunInfo> GunInfos
        {
            get { return mGunInfos; }
        }

        public GunInfo CurrentGun { get; } = new GunInfo()
        {
            BulletCountInGun = new BindableProperty<int>()
            {
                Value = 3
            },
            BulletCountOutGun = new BindableProperty<int>()
            {
                Value = 1
            },
            Name = new BindableProperty<string>()
            {
                Value = "手枪"
            },
            State = new BindableProperty<GunState>()
            {
                Value = GunState.Idle
            }
        };
        
        public void PickGun(string name, int bulletCountInGun, int bulletCountOutGun)
        {
            // 当前枪是同类型
            if (CurrentGun.Name.Value == name)
            {
                CurrentGun.BulletCountOutGun.Value += bulletCountInGun;
                CurrentGun.BulletCountOutGun.Value += bulletCountOutGun;
            }
            // 已经拥有这把枪了
            else if (mGunInfos.Any(info => info.Name.Value == name))
            {
                var gunInfo = mGunInfos.First(info => info.Name.Value == name);
                gunInfo.BulletCountOutGun.Value += bulletCountInGun;
                gunInfo.BulletCountOutGun.Value += bulletCountOutGun;
            }
            else
            {
                EnqueueCurrentGun(name,bulletCountInGun,bulletCountOutGun); // -+
            }
        }

        void EnqueueCurrentGun(string nextGunName, int nextGunBulletCountInGun, int nextGunBulletCountOutGun) // +
        {
            // 复制当前的枪械信息
            var currentGunInfo = new GunInfo
            {
                Name = new BindableProperty<string>()
                {
                    Value = CurrentGun.Name.Value
                },
                BulletCountInGun = new BindableProperty<int>()
                {
                    Value = CurrentGun.BulletCountInGun.Value
                },
                BulletCountOutGun = new BindableProperty<int>()
                {
                    Value = CurrentGun.BulletCountOutGun.Value
                },
                State = new BindableProperty<GunState>()
                {
                    Value = CurrentGun.State.Value
                }
            };

            // 缓存
            mGunInfos.Enqueue(currentGunInfo);

            // 新枪设置为当前枪
            CurrentGun.Name.Value = nextGunName;
            CurrentGun.BulletCountInGun.Value = nextGunBulletCountInGun;
            CurrentGun.BulletCountOutGun.Value = nextGunBulletCountOutGun;
            CurrentGun.State.Value = GunState.Idle;

            // 发送换枪事件
            this.SendEvent(new OnCurrentGunChanged()
            {
                Name = nextGunName
            });
        }

        public void ShiftGun()
        {
            if (mGunInfos.Count > 0)
            {
                var nextGunInfo =  mGunInfos.Dequeue();

                EnqueueCurrentGun(nextGunInfo.Name.Value,nextGunInfo.BulletCountInGun.Value,nextGunInfo.BulletCountOutGun.Value); // -+
            }
        }
    }
}